package com.sd.repay.transfer;

import com.sd.repay.common.CommonResult;
import com.sd.repay.pojo.TransferOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 出款
 *
 * @author sd
 * @date 2018年9月6日17:39:28
 */
@Service
public class Transfer {

    private Logger log = LoggerFactory.getLogger(Transfer.class);

    @Autowired
    private TransferService transferService;

    @Resource
    private BaseBankRouteService bankRouteService;


    /**
     * 出款
     *
     * @param transferOrder 提交出款信息
     * @return
     */
    public TransferOrder pay(TransferOrder transferOrder) {
        log.info("接收出款请求[{}]", transferOrder.toJsonString());
        TransferOrder returnOrder = new TransferOrder();

        TransferCheckUtils checkUtils = new TransferCheckUtils();
        CommonResult result = checkUtils.basicsTransferOrderCheck(transferOrder);

        if (result.getStatus() == 2) {
            returnOrder.setStatus(String.valueOf(result.getStatus()));
            returnOrder.setSubResultMsg(result.getMsg());
            log.info("请求出款明细[{}] 响应[{}]", transferOrder.toJsonString(), returnOrder.toJsonString());
            return returnOrder;
        }

        TransferOrder extOrder = transferService.getTransferOrder(transferOrder.getOrderType(), transferOrder.getSourceOrderNo(), null);
        if (extOrder != null) {
            returnOrder.setStatus("2");
            returnOrder.setSubResultMsg("订单已存在");
            log.info("请求出款明细[{}] 响应[{}]", transferOrder.toJsonString(), returnOrder.toJsonString());
            return returnOrder;
        }

        returnOrder = bankRouteService.pay(transferOrder);

        log.info("请求出款明细[{}] 响应[{}]", transferOrder.toJsonString(), returnOrder.toJsonString());
        return returnOrder;
    }


    /**
     * 同步出款状态
     * <p>
     * <p>
     * 同步出款状态，day天数内，minute 分钟以外(建议5分钟以外)
     *
     * @param day    天数
     * @param minute 分钟
     */
    public void sysTransferOrder(int day, int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();

        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        nowCal.add(Calendar.DAY_OF_YEAR, -day);
        String beginTime = sdf.format(nowCal.getTime()) + " 00:00:00";

        nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        nowCal.add(Calendar.MINUTE, -minute);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format(nowCal.getTime());
        log.info("同步出款状态开始执行，本次同步[{}] 天内， [{}]分钟以外的订单", day, minute);

        List<TransferOrder> orderList = new ArrayList<>();
        orderList = transferService.getTransferOrderListByTime(beginTime, endTime, "1", "6");
        log.info("本次共检索到[{}]条需要同步的出款记录", orderList.size());

        int successCount = 0;
        int failCount = 0;
        int UnknownCount = 0;
        for (int i = 0; i < orderList.size(); i++) {
            TransferOrder transferOrder = orderList.get(i);
            bankRouteService.sysTransferOrderStatus(transferOrder);
            String status = transferOrder.getStatus();
            String subOrderNo = transferOrder.getSubOrderNo();
            String payBankNo = transferOrder.getOutPayBank();
            String resultCode = transferOrder.getResultCode();
            String resultMsg = transferOrder.getResultMsg();

            if ("4".equals(status)) {
                successCount++;
            } else if ("5".equals(status)) {
                failCount++;
            } else {
                UnknownCount++;
            }
            log.info("本次需要同步[{}]条记录，当前同步第[{}]条，出款流水号[{}]，渠道[{}]，同步结果[{}]，渠道响应码[{}]响应描述[{}]", new Object[]{orderList.size(), i + 1, subOrderNo, payBankNo, status, resultCode, resultMsg});
        }
        log.info("本次同步任务完成，共同步[{}]条记录，交易成功[{}]条，失败[{}]条，处理中[{}]条", new Object[]{orderList.size(), successCount, failCount, UnknownCount});
    }
}
